package com.example.cinema.request;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.example.cinema.bindingdata.LiveTableData;
import com.example.cinema.bindingdata.VideoData;
import com.example.cinema.databinding.ActivityPlayerBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RequestTypeList {
    private String url;
    private String purl;
    public RequestTypeList(Context context) {
        try {
            ApplicationInfo appinfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            url = appinfo.metaData.getString("url");
            purl= appinfo.metaData.getString("playurl");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }

    public boolean GetVideo(String p,String l,String tid,List<VideoData> videolist)
    {
        boolean reltus = false;
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder().add("page",p).add("limit",l).add("type",tid).build();
        Request request = new Request.Builder().url(url+"/com/typevideo").post(formBody).build();
        try {
            Response response = client.newCall(request).execute();
            String bod = response.body().string();
            Log.i("body",bod);
            JSONObject ob = new JSONObject(bod);
            JSONArray array = ob.getJSONArray("data");
            if(array.length()>0)
            {
                reltus = true;
            }
            List<VideoData> listvideo = new ArrayList<>();
            for (int i=0;i<array.length();i++)
            {
                JSONObject jso = array.getJSONObject(i);
                VideoData videoData = new VideoData();
                videoData.videosid.set(jso.getString("videosid"));
                videoData.videoname.set(jso.getString("videoname"));
                videoData.videotype.set(jso.getString("videotype"));
                videoData.videourls.set(jso.getString("videopath"));
                Log.i("GRRR",jso.getString("videopath"));
                videoData.videotime.set(ConvertTime(jso.getString("videotime")));
                Log.i("HHHHDD","=====1");
                videoData.videobrief.set(jso.getString("videobrief")) ;
                Log.i("HHHHDD","=====2");
                videoData.videogif.set(jso.getString("videogif"));
                Log.i("HHHHDD","=====3");
                videoData.videoimge.set(jso.getString("videoimg"));
                Log.i("HHHHDD","=====4");
                videoData.videotage.set(jso.getString("videotag"));
                Log.i("HHHHDD","=====5");
                videoData.videoimge.set(jso.getString("videocover"));
                Log.i("HHHHDD","=====6");
                videoData.videouser.set(jso.getString("videouser"));
                Log.i("HHHHDD","=====7");
                videoData.videousid.set(jso.getString("videousid"));
                Log.i("HHHHDD","=====8");
                videoData.videopart.set(jso.getString("videopart"));
                Log.i("HHHHDD","=====9");
                videoData.videolove.set(jso.getString("videolove"));
                Log.i("HHHHDD","=====10");
//                videoData.videouimg.set(jso.getString("videouimg"));
                Log.i("HHHHDD","=====11");
                videoData.videouptime.set(jso.getString("videouptime"));
                Log.i("HHHHDD","=====12");
                listvideo.add(videoData);
                Log.i("HHHHDD","=====");
            }
            Log.i("HHHHDD","++++");
            videolist.addAll(listvideo);
            Log.i("HHHHDD",videolist.size()+"数量");
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return reltus;
    }


    //时间转换
    public String ConvertTime(String time)
    {
        String tm = "";
        int t = Integer.valueOf(time);
        int s = t/60;
        if(s<1)
        {
            tm = "1分钟";
        }else {
            tm = String.valueOf(s)+"分钟";
        }
        return tm;
    }
}
